<template>
  <div class="wen-box">
    <div class="fabu">
      <div class="wenzhangk">
        <span>标签名称</span>
        <el-input placeholder="请输入（模糊查询）" v-model="input" clearable>
        </el-input>
      </div>
      <div class="block">
        <span class="demonstration">创建日期</span>
        <el-date-picker
          v-model="value2"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </div>
      <el-button @click="chaxun" type="primary"
        ><i class="el-icon-search"></i>查询</el-button
      >
      <el-button @click="chognzhi"
        ><i class="el-icon-refresh-left"></i>重置</el-button
      >
    </div>
    <div>
      <div class="zengjia">
        <el-button @click="dialogTableVisible = true" type="primary"
          >+增加</el-button
        >
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="名称" width="180">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>{{ scope.row.name }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.name }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="新增分类" :visible.sync="dialogTableVisible">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item
          label="分类名称"
          prop="name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
      },
      input: "",
      tagName: "",
      current: 1,
      size: 10,
      total: 0,
      tableData: [],
      dialogTableVisible: false,
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getFen();
    // this.getzen();
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.getFen();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val;
      this.getFen();
    },
    async getzen() {
      let res = await this.$axios.post("/admin/category/add", {
        name: this.form.name,
      });
      console.log(res);
      this.getFen();
    },
    submitForm() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          // 校验通过，调用新增接口
          await this.getzen();
          this.dialogTableVisible = false; // 关闭弹窗
          this.form.name = ""; // 清空表单
        } else {
          // 校验失败，不提交
          console.log("表单校验失败，无法提交");
          return false;
        }
      });
    },
    // zenjia() {},
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getdel(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async getdel(articleId) {
      try {
        let res = await this.$axios.post("/admin/category/delete", {
          categoryId: articleId,
        });
        if (res.data.success) {
          // 假设接口返回success标识成功
          this.$message.success("删除成功");
          this.getFen(); // 重新加载列表
        }
      } catch (error) {
        this.$message.error("删除失败");
      }
    },
    chaxun() {
      this.current = 1;
      this.getFen();
    },
    chognzhi() {
      this.value2 = "";
      this.input = "";
      this.from.current = 1; // 重置页码
      this.from.size = 10; // 重置每页条数
      this.getFen();
    },
    async getFen() {
      let res = await this.$axios.post("/admin/tag/list", {
        current: this.current,
        size: this.size,
        tagName: this.input,
      });
      console.log(res);
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
    },
  },
};
</script>

<style>
.wen-box {
  margin-top: 4.375rem;
}
.fabu {
  line-height: 2.8125rem;
  text-align: left;
  background-color: #fff;
  margin: 1.25rem auto;
  padding: 1.25rem;
  display: flex;
  justify-content: flex-start;
  gap: 1rem;
}
.wenzhangk {
  display: flex;
}
.wenzhangk span {
  width: 6.5rem;
}
.el-table {
  line-height: 2.875rem;
}
.demonstration {
  margin-right: 1.25rem;
}
.el-icon-search {
  margin-right: 0.625rem;
}
.el-icon-refresh-left {
  margin-right: 0.625rem;
}
.zengjia {
  background-color: #fff;
  text-align: left;
  /* margin: 0 0 0 1.25rem; */
  line-height: 2.8125rem;
}
.zengjia button {
  /* margin-left: 1.25rem; */
  margin: 1.25rem 1.25rem;
}
.html {
  line-height: 0pxf;
}
</style>